<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/9/15
 * Time: 20:58
 */

namespace App\Services;

use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Component\TableManager;
/**推送消息
 * Class PushMessageService
 * @package App\Services
 */
class PushMessageService
{
    /**给本机连接用户推送消息
     * @param $fd
     * @param array $messageArr
     * @return mixed
     */
    public function push($fd,array $messageArr,$whiteList=false){
        $server = ServerManager::getInstance()->getSwooleServer();
        if($whiteList==false){
            $clientInfo =  $server->getClientInfo($fd);
            if(empty(   $clientInfo )){
                return false;
            }
            #校验用户是否就绪
            $readyTable =  TableManager::getInstance()->get(Config::getInstance()->getConf('CHAT.user_ready'));
            $readyInfo = $readyTable->get($clientInfo['uid']);
            if(empty($readyInfo)){
                return false;
            }
        }

        $fdToAppTable = TableManager::getInstance()->get(Config::getInstance()->getConf('CHAT.fd_to_app'));
        $fdToApp =  $fdToAppTable->get($fd);
        return    $server->push($fd, HelpService::aesEncrypt(json_encode($messageArr), $fdToApp['access_key'], $fdToApp['secret_key']));
    }
}